/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

package rpt.bd.cenario;

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Roberto
*/

/**************************************************************************/
/***                                                                    ***/
/**************************************************************************/

public class CEstruturaRegistroBdCenarioEdicao extends CEstruturaRegistroBdCenario {

    CCenarioBdEdicao fCenarioBdEdicao;

    public CEstruturaRegistroBdCenarioEdicao(
        CCenarioBdEdicao aCenarioBdEdicao) {
        super(aCenarioBdEdicao);
        fCenarioBdEdicao = aCenarioBdEdicao;
        fGlobal = fCenarioBdEdicao.getGlobal();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaRegistroBdCenarioEdicao(
        CCenarioBdEdicao aCenarioBdEdicao,
        Object aCondicao) {
        //
        this(aCenarioBdEdicao);
        fCondicao = aCondicao;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaRegistroBdCenarioEdicao(
            CCenarioBdEdicao aCenarioBdEdicao,
            String aNomeChavePrimaria,
            String aNomeCampoDescricao,
            Object aCondicao) {
        this(aCenarioBdEdicao,aCondicao);
        fNomeChavePrimaria = aNomeChavePrimaria;
        fNomeCampoDescricao = aNomeCampoDescricao;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CEstruturaRegistroBdCenarioEdicao(
            CCenarioBdEdicao aCenarioBdEdicao,
            String aNomeChavePrimaria,
            String aNomeCampoDescricao) {
        this(aCenarioBdEdicao);
        fNomeChavePrimaria = aNomeChavePrimaria;
        fNomeCampoDescricao = aNomeCampoDescricao;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    protected void eventoGanhaFoco(FocusEvent evt) {
        //jTextField1FocusGained(evt);
        Object o = evt.getSource();
        if (o instanceof JComponent) {
            if (fComponenteFocado ==  (JComponent)o) {
                return;
            }
            fComponenteFocado = (JComponent)o;
            CEstruturaCampoRegistroBd e = getEstruturaCampoRegistroBdComponente(fComponenteFocado);
            if (e != null) fIndiceCampoFocado = getIndiceEstruturaCampoRegistroBd(e);
            CGradeBdCenario g = null;
            if (fCenarioBdEdicao != null) {
                //CRegistroBd r = fCenarioBdEdicao.getRegistroBdAtual();
                //CCampoRegistroBd cr = r.atualizarCampoRegistroBd(fComponenteFocado,null);
                g = fCenarioBdEdicao.getGrade();
                if (g != null) {
                    EStatusOperacao so = fCenarioBdEdicao.getPertinenciaBd().getStatusOperacao();
                    if ((so != EStatusOperacao.inclusao) && (so != EStatusOperacao.alteracao)) {
                        e.setCores(false,false);
                        g.requestFocus();
                    }
                    else {
                        fComponenteFocado.setBackground(new Color(192,255,192));
                        fComponenteFocado.setForeground(new Color(0,0,0));
                        //Component c1 = evt.getComponent();
/**/
                        Component c1 = fComponenteFocado;
                        Component c2 = c1.getParent();
                        if (c2 instanceof JComponent) {
                            JComponent jc1 = (JComponent)c2;
                            Rectangle r1 = c1.getBounds(null);
                            jc1.scrollRectToVisible(r1);
                            jc1.repaint();
                            Component c3 = jc1.getParent();
                            if (c3 instanceof JComponent) {
                                JComponent jc2 = (JComponent)c3;
                                Rectangle r2 = jc1.getBounds(null);
                                jc2.scrollRectToVisible(r2);
                                jc2.repaint();
                                Component c4 = jc2.getParent();
                                if (c4 instanceof JComponent) {
                                    JComponent jc3 = (JComponent)c4;
                                    Rectangle r3 = jc2.getBounds(null);
                                    jc3.scrollRectToVisible(r3);
                                    jc3.repaint();
                                }
                            }
                        }
/**/
                    }
                }
            }
        }
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    protected void eventoPerdeFoco(FocusEvent evt) {
        Object o = evt.getSource();
        if (o instanceof Component) {
            Component vComponente = (Component)o;
            //if (fComponenteFocado !=  vComponente) return;
            //vComponente.setBackground(new Color(255,255,255));
            //vComponente.setForeground(new Color(0,0,0));
            if (fCenarioBdEdicao != null) {
                CRegistroBd r = fCenarioBdEdicao.getRegistroBdAtual();
                CCampoRegistroBd cr = null;
                if (r != null)
                    cr = r.atualizarCampoRegistroBd(vComponente,null);
                CGradeBdCenario g = fCenarioBdEdicao.getGrade();
                if (g != null) {
                        //CRegistroBd r = g.getRegistroBdAtual();
                        EStatusOperacao so = fCenarioBdEdicao.getPertinenciaBd().getStatusOperacao();
                        boolean vConsulta = (so != EStatusOperacao.inclusao) && (so != EStatusOperacao.alteracao);
                        if (vConsulta) {
                            //e.setCores(false,false);
                            g.requestFocus();
                        }
                        else {
                            g.atualizarCampoRegistroBd(r,vComponente,null);
                            //vComponente.setBackground(new Color(255,255,255));
                            //vComponente.setForeground(new Color(0,0,0));
                    }
                }
            }
        }
    }
    
    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    public void componentPropertyChange(java.beans.PropertyChangeEvent evt) {
        if (!evt.getPropertyName().toLowerCase().equals("value")) return;
/*
        Object o = evt.getSource();
        if (o instanceof Component) {
            Component vComponente = (Component)o;
            //if (fComponenteFocado !=  vComponente) return;
            //vComponente.setBackground(new Color(255,255,255));
            //vComponente.setForeground(new Color(0,0,0));
            if (fCenarioBdEdicao != null) {
                CRegistroBd r = fCenarioBdEdicao.getRegistroBdAtual();
                CCampoRegistroBd cr = r.atualizarCampoRegistroBd(vComponente,null);
                CGradeBdCenario g = fCenarioBdEdicao.getGrade();
                if (g != null) {
                        //CRegistroBd r = g.getRegistroBdAtual();
                        EStatusOperacao so = fCenarioBdEdicao.getPertinenciaBd().getStatusOperacao();
                        boolean vConsulta = (so != EStatusOperacao.inclusao) && (so != EStatusOperacao.alteracao);
                        if (vConsulta) {
                            //e.setCores(false,false);
                            g.requestFocus();
                        }
                        else {
                            g.atualizarCampoRegistroBd(r,vComponente,null);
                            //vComponente.setBackground(new Color(255,255,255));
                            //vComponente.setForeground(new Color(0,0,0));
                    }
                }
            }
        }
*/
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public void setCenario (CCenarioBdEdicao aCenarioBd) {
        fCenarioBdEdicao = aCenarioBd;
        fGlobal = aCenarioBd.getGlobal();
        fPersistenciaBd = aCenarioBd.getPertinenciaBd();
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public CCenarioBd getCenarioBd() {
        return fCenarioBdEdicao;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    public boolean focarComponenteAnterior() {
        boolean vEncontrou = false;
        for (int vContador = (fIndiceCampoFocado-1);((vContador >= 0) && (!vEncontrou)); vContador--) {
            CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            if (vEstruturaCampoRegistroBd.getCampoEditavel()) {
                if (fCenarioBdEdicao.getPainelBd().componenteVisualLiberado(vEstruturaCampoRegistroBd)) {
                    fIndiceCampoFocado = vContador;
                    vEstruturaCampoRegistroBd.setFoco();
                    vEncontrou = true;
                }
            }
        }
        if (!vEncontrou) {
            final int vNumCampos = fCamposEstruturaRegistroBd.size();
            for (int vContador = (vNumCampos-1);((vContador > fIndiceCampoFocado) && (!vEncontrou)); vContador--) {
                CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
                if (vEstruturaCampoRegistroBd.getCampoEditavel()) {
                    fIndiceCampoFocado = vContador;
                    vEstruturaCampoRegistroBd.setFoco();
                    vEncontrou = true;
                }
            }
        }
        return vEncontrou;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    public boolean focarProximoComponente() {
        boolean vEncontrou = false;
        int vNumCampos = fCamposEstruturaRegistroBd.size();
        for (int vContador = (fIndiceCampoFocado+1);((vContador < vNumCampos) && (!vEncontrou)); vContador++) {
            CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
            if (vEstruturaCampoRegistroBd.getCampoEditavel()) {
                if (fCenarioBdEdicao.getPainelBd().componenteVisualLiberado(vEstruturaCampoRegistroBd)) {
                    fIndiceCampoFocado = vContador;
                    vEstruturaCampoRegistroBd.setFoco();
                    vEncontrou = true;
                }
            }
        }
        if (!vEncontrou) {
            for (int vContador = 0;((vContador < fIndiceCampoFocado) && (!vEncontrou)); vContador++) {
                CEstruturaCampoRegistroBd vEstruturaCampoRegistroBd = (CEstruturaCampoRegistroBd)fCamposEstruturaRegistroBd.get(vContador);
                if (vEstruturaCampoRegistroBd.getCampoEditavel()) {
                    fIndiceCampoFocado = vContador;
                    vEstruturaCampoRegistroBd.setFoco();
                    vEncontrou = true;
                }
            }
        }
        return vEncontrou;
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    public void eventoAntesAtualizarPainelBd(CRegistro aRegistro) {
        super.eventoAntesAtualizarPainelBd(aRegistro);
        fCenarioBdEdicao.getPainelBd().eventoAntesAtualizarPainelBd(aRegistro);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    @Override
    public void eventoDepoisAtualizarPainelBd(CRegistro aRegistro) {
        super.eventoDepoisAtualizarPainelBd(aRegistro);
        fCenarioBdEdicao.getPainelBd().eventoDepoisAtualizarPainelBd(aRegistro);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

    public String getQuerySelecionar(final String aCondicaoExterna) {
        return fCenarioBdEdicao.getPainelBd().getQuerySelecionar(aCondicaoExterna);
    }

    /**************************************************************************/
    /***                                                                    ***/
    /**************************************************************************/

}
